/*
 * lab2.fx
 * Created on 22.09.2009, 16:20:32
 */

package javafx179labs.nez.lab2;

/**
 * @author civil
 */

// place your code here

//Пофиксил ворнинги ^____^
//Хорошо получилось.
//Чтобы вы не скучали почти две недели, могу вам доп. задачу выдать для развития ваших способностей.
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.ext.swing.SwingTextField;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;

def bheight:Float = 21;
def lbwidth:Float = 28;
def rbwidth:Float = 40;
function makeline (text:String) {
    var txt = text;
    var mode: Group;
    var edit: Group;


    var efield = SwingTextField {
        columns: 27
        text: txt
    }

    var main = Group {
        content: [
                //text zone
                SwingTextField {
                    editable: false
                    columns: 27
                    text: bind txt
                },

                Button {
                    text: "edit"
                    height: bheight
                    width: lbwidth
                    layoutX: 305
                    action: function () {
                        efield.text = txt;
                        mode = edit
                    }
                }
        ]
    }

    edit = Group {
        content: [
                efield,

                Button {
                    text: "ok"
                    height: bheight
                    width: lbwidth
                    layoutX: 305
                    action: function () {
                        txt = efield.text;
                        mode = main
                    }
                },

                Button {
                    text: "cancel"
                    height: bheight
                    width: rbwidth
                    layoutX: 338
                    action: function () {
                        mode = main
                    }
                }
        ]
    }

    mode = main;

    Group {
        content: bind [mode]
    }

}

function lines (str_collect: String[]) {//а как же camelCase? Я не придираюсь, в Джаве есть определенные соглашения, которые лучше соблюдать. Java code convention
    var strs = str_collect;
    for (elem in strs) {
        Group {
            content: [
                    makeline(elem)//и здесь camelCase
            ]
        }
    }
}

function drawlines (lns: Group[]) {//drawLines
    var lines: Group[];

    var addfield = SwingTextField {
        columns: 27
    }

    var newline = function (line: Group) {//:)
        var ngr:Group = Group {
            translateX: 5
            content: [
                    bg(),
                    Button {
                        text: "delete"
                        height: bheight
                        width: rbwidth
                        layoutX: 338
                        action: function () {
                            lines = lines[x| x != ngr];
                        }
                    },
                    line
            ]
        }
    };

    lines = for (line in lns) {
        newline(line)
    }

    Group {
        translateX: 3
        content: [
                Group {
                    translateY: 5
                    translateX: 5
                    content: [
                            bg(),
                            addfield,
                            Button {
                                text: "add string"
                                height: bheight
                                width: 73
                                layoutX: 305
                                action: function () {
                                    if (addfield.text != "") {
                                        insert newline(makeline(addfield.text)) into lines;
                                        addfield.text = ""
                                    }
                                }
                            }
                    ]
                },

                Group {
                    content: bind function (lins: Group[]) {
                        var vs = 5;
                        for (line in lins) {
                            line.translateY = (vs += 28)
                        }
                        lins
                    } (lines)
                }
        ]
    }
}


function bg () {

    Rectangle {
        x: -4
        y: -4
        width: 385
        height: 26
        arcHeight: 7
        arcWidth: 7
        stroke: Color.BLACK
        fill: Color.AZURE
    }
}


Stage {
    title: "o_O"
    width: 396
    height: 600
    scene: Scene {
        content: [drawlines(lines(["ляляля", "ня ня ня", "ыыы"]))]
        fill: Color.DARKGREY
    }

}

